<?php

  /*

      TravianWAP
      Copyright (c) 2007-08 by Ananas - All rights reserved
      http://n00bz.biz/travianwap/
      Contact: uroskn@gmail.com

      --------------------------------------------------------------------------

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU  General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU  General Public License
      along with this program. If not, see http://www.gnu.org/licenses/.

  */

  $tnames = array(
    array(),
    array("", "Legionnaire", "Praetorian", "Imperian", "Equites Legati", "Equites Imperatoris", "Equites Caesaris", "Battering Ram", "Fire Catapult", "Senator", "Settler", "Hero"),
    array("", "Phalanx", "Swordsman", "Pathfinder", "Theutates Thunder", "Druidrider", "Haeduan", "Ram", "Trebuchet", "Chieftain", "Settler", "Hero"),
    array("", "Clubswinger", "Spearman", "Axeman", "Scout", "Paladin", "Teutonic Knight", "Ram", "Catapult", "Chief", "Settler", "Hero"),
    array("", "Rat", "Spider", "Serpent", "Bat", "Wild boar", "Wolf", "Bear", "Crocodile", "Tiger", "Elephant"),
    array("", "Pikeman", "Thorned Warrior", "Guardsman", "Birds Of Prey", "Axerider", "Natarian Knight", "Warelephant", "Ballista", "Natarian Emperor", "Settler", "Hero")
  );

  define("MODULE_OK", 1);
  if (!defined("MAIN_UNIT")) die("h4xx0r");
  
  if (empty($req["page"])) $page = 0;
  else $page = $req["page"];

  echo "<b>Reports</b><br/>";
  
  if (empty($req["details"]))
  {
    $info = travian_return_reports($_SESSION, False, $req["num"], $page);;
    if (!empty($req["delall"]))
    {
      for ($i = 0; $i < count($info); $i++) $request["n".($i + 1)] = $info[$i]["ivalue"];
      $request["del"] = "Remove";
      $text = http_post("http://{$_SESSION["serv"]}/berichte.php", $request);
      $text = $text["body"];
      $info = travian_return_reports($text, True);
    }
    if (!empty($req["rem"]))
    {
      $request["del"] = "remove";
      $request["n1"] = $req["id"];
      $text = http_post("http://{$_SESSION["serv"]}/berichte.php", $request);
      $text = $text["body"];
      $info = travian_return_reports($text, True);
    }
    
    //print_r($info);
    if (empty($info[0]["ivalue"]))
    {
      echo "<br/>No reports!";
      $p = $page - 1;
      if ($page != 0)
      {
        echo "<br/><br/>";
        print_link("main.php?mod=reports&amp;page=$p".ps(), "Previous");
      }
    }
    else
    {
      for ($i = 0; $i < count($info); $i++)
      {
        $new = '<br/>[UNREAD REPORT]';
        if (!$info[$i]["new"]) $new = '';
        echo "<br/>".date("d.m.y H:i", $info[$i]["date"])."<br/>";
        print_link("main.php?mod=reports&amp;id={$info[$i]["ivalue"]}&amp;rem=1".ps(), "[X]", "&nbsp;");
        echo "$new<br/>".print_link("main.php?mod=reports&amp;id={$info[$i]["ivalue"]}&amp;details=1".ps(), $info[$i]["title"]);
      }

      $p = $page - 1;
      $n = $page + 1;
      if ($page != 0) echo print_link("main.php?mod=reports&amp;page=$p".ps(), "Previous", " - ");
      if (count($info) == 10) print_link("main.php?mod=reports&amp;page=$n".ps(), "Next");
      echo "<br/>";
      print_link("main.php?mod=reports&amp;delall=1&amp;page=$page".ps(), "Delete all");
    }
  }
  else
  {
    $info = travian_return_report_info($_SESSION, $req["id"]);
    //print_r($info);
    echo "<br/><b>{$info["title"]}</b><br/>".date("d.m.y H:i", $info["date"])."<br/>";
    if ($info["trade"]) echo "{$info["player"]["name"]} from {$info["village"]["name"]}<br/>".
                             "{$info["resource"][0]} {$info["resource"][1]} {$info["resource"][2]} {$info["resource"][3]}<br/>";
    else
    {
      if ($info["type"] != 2)
      {
        echo "<br/>Player: {$info[0]["player"]["name"]}<br/>Village: {$info[0]["village"]["name"]}<br/>";
        for ($x = 0; $x <= 11; $x++) if (!empty($info[0]["troops"][$x])) echo $tnames[$info[0]["ttype"]][$x + 1].": ".$info[0]["troops"][$x]."/".$info[0]["casualties"][$x]."/".$info[0]["prisoners"][$x]."<br/>";
        for ($x = 0; $x <= 5; $x++) if (!empty($info[0]["info"][$x])) echo "Info: {$info[0]["info"][$x]}<br/>";
        echo "{$info[0]["taken"][0]}/{$info[0]["taken"][1]}/{$info[0]["taken"][2]}/{$info[0]["taken"][3]}<br/><br/>";
        for ($i = 1; $i <= 3; $i++)
        {
          if (!empty($info[$i]["ttype"]))
          {
            echo "Defender: {$info[$i]["player"]["name"]}<br/>Village: {$info[$i]["village"]["name"]}<br/>";
            for ($x = 0; $x <= 11; $x++) if (!empty($info[$i]["troops"][$x]))
            {
              echo $tnames[$info[$i]["ttype"]][$x + 1].": ".$info[$i]["troops"][$x];
              if (!empty($info[$i]["casualties"][$x])) echo "/".$info[$i]["casualties"][$x];
              echo "<br/>";
            }
            for ($x = 0; $x <= 5; $x++) if (!empty($info[$i]["info"][$x])) echo "Info: {$info[$i]["info"][$x]}<br/>";
            echo "<br/>";
          }
        }
      }
      else
      {
        echo "<br/>Player: {$info["player"]["name"]}<br/>Village: {$info["village"]["name"]}<br/>";
        for ($x = 0; $x <= 11; $x++) if (!empty($info["troops"][$x])) echo $tnames[$info["ttype"]][$x + 1].": ".$info["troops"][$x]."<br/>";
        echo "Crop usage: ".$info["cropuse"]."<br/><br/>";
      }
    }
    print_link("main.php?mod=reports".ps(), "Back");
  }

  echo "<br/><br/>";
